{extend name="$base" /}
{block name="resources"}
<style>
.modal-infp-style{width:90%;margin:10px auto;}
.modal-infp-style table{width:100%;}
.modal-infp-style table tr td{border: 1px solid #e5e5e5;padding: 10px;}
.align_left{text-align: left;}
.align_right{text-align: right;}
</style>
{/block}
{block name="main"}
<table class="mytable">
	<tr>
		<th width="20%" style="text-align:left;">
			<a class="btn-common" href="javascript:;" onclick="setAppletCategory()">设置分类</a>
		</th>
		<th width="10%">
			<input type="text" id = 'category_name' placeholder="请输入分类名称" class="input-common" />
			<select class="select-common" id="applet_id_search">
				<option value="">请选择分类归属</option>
				<option value="0">不归属任何小程序</option>
				{volist name="$applet_list" id="applet"}
				<option value="{$applet.applet_id}">{$applet.applet_name}</option>
				{/volist}
			</select>
			<input type="button" onclick="searchData()" value="搜索" class="btn-common" />
		</th>
	</tr>
</table>
<table class="table-class">
		<col style="width: 2%;">
		<col style="width: 30%;">
		<col style="width: 68%;">
	<thead>
		<tr align="center">
			<th class="align_center">
				<i class="checkbox-common" input_all ="#productTbody">
					<input type="checkbox" id="check_all">
				</i>
			</th>
			<th class="align_left">分类名称</th>
			<th class="align_left">所属小程序</th>
		</tr>
	</thead>
	<tbody></tbody>
</table>

<!-- 账户调整 -->
<div class="modal fade hide" id="setAppletCategoryModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">设置分类</div>
			<div class="modal-body">
				<div class="set-style">
					<dl>
						<dt style="width:23% !important;"><span class="color-red">*</span>选择分类归属：</dt>
						<dd>
							<select class="" id="applet_id_set">
								<option value="0">不归属任何小程序</option>
								{volist name="$applet_list" id="applet"}
								<option value="{$applet.applet_id}">{$applet.applet_name}</option>
								{/volist}
							</select>
						</dd>
					</dl>
				</div>
			</div>
			<div class="modal-footer">
				<input type="hidden" id="point_id" />
				<button class="btn-common btn-big" data-type="submit">保存</button>
				<button class="btn-common-cancle btn-big" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
function LoadingInfo(page_index) {
	var category_name = $("#category_name").val();
	var applet_id = $("#applet_id_search").val();
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/wchat/categoryList')}",
		data : {
			"page_index" : page_index,
			"page_size" : $("#showNumber").val(),
			"category_name" : category_name,
			"applet_id" : applet_id
		},
		success : function(data) {
			if (data["data"].length > 0) {
				$(".table-class tbody").empty();
				for (var i = 0; i < data["data"].length; i++) {
					var html = '';
					html += '<tr align="center">';
						html += '<td align="center">';
							html += '<i class="checkbox-common"><input value="' + data["data"][i]["category_id"] + '" name="sub" data-state="'+data["data"][i]["state"]+'" type="checkbox"></i>';
						html += '</td>';
                    	html += '<td class="align_left">' + data["data"][i]["category_name"] + '</td>';
						html += '<td class="align_left">' + (data["data"][i]["applet_id"] ? data["data"][i]["applet_name"] : '--') + '</td>';
					html += '</tr>';
					$(".table-class tbody").append(html);
				}
			} else {
				var html = '<tr align="center"><td colspan="3">暂无符合条件的数据记录</td></tr>';
				$(".table-class tbody").html(html);
			}
			initPageData(data["page_count"],data['data'].length,data['total_count']);
			$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
		}
	});
}

function searchData(){
	LoadingInfo(1);
}

function setAppletCategory(){
	var id_arr = getCategoryId();
	if(id_arr.length <= 0){
	    showTip('请选择分类', 'warning');
	    return false;
	}

	$("#setAppletCategoryModal").modal('show');
    $("#setAppletCategoryModal .modal-footer button[data-type='submit']").unbind('click').bind('click', function(){
        var applet_id = $("#applet_id_set").val();
        $.ajax({
            type : "post",
            url : "{:__URL('ADMIN_MAIN/wchat/setAppletCategory')}",
            data : {
                "applet_id" : applet_id,
                "category_ids" : id_arr.toString()
			},
            success : function(data){
                if (data["code"] > 0) {
                    showTip('设置成功',"success");
                    LoadingInfo($("#pageNumber a").html());
                    $("#setAppletCategoryModal").modal('hide');
                }else{
                    showTip(data['message'],"error");
                }
            }
        })
	})
}

//获取选择的分类
function getCategoryId(){
    var dom_arr = $(".table-class tbody input[type='checkbox']:checked");
    var id_arr = [];
    dom_arr.each(function(index, dom){
        id_arr.push($(dom).val());
	})
	return id_arr;
}
</script>
{/block}